package com.qin.EducationalManger.utils;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;

import javax.management.Query;
import javax.swing.text.DefaultEditorKit.InsertBreakAction;

import com.qin.EducationalManger.student.model.StudentModel;

public final class JdbcUtils {

	private void JdbcUtils() {

	}

	private static ResourceBundle rb = ResourceBundle.getBundle("com.qin.StudentManger.utils.jdbc");

	private static String driver = rb.getString("driver");
	private static String url = rb.getString("url");
	private static String username = rb.getString("username");
	private static String password = rb.getString("password");

	// 驱动加载 ， 每当创建这个类的对象就会加载驱动
	static {
		try {
			Class.forName(driver);
			System.out.println("驱动加载成功");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.err.println("驱动加载失败");
		}

	}

// 获得连接
	public static Connection getConnection() {
		Connection connection = null;

		try {
			connection = DriverManager.getConnection(url, username, password);
			System.out.println("连接成功");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.err.println("连接失败");
		}

		return connection;

	}

	// 关闭
	public static void exclose(ResultSet rs, Statement statement, Connection con) {
		if (rs != null) {
			try {
				rs.close();
				rs = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (statement != null) {
			try {
				statement.close();
				statement = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (con != null) {
			try {
				con.close();
				con = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	// 对数据库的增删/改/操作
	public static Integer update(String sql, List<Object> vals) {
		Connection con = getConnection();
		PreparedStatement ps = null;

		con = JdbcUtils.getConnection();
		try {
			ps = con.prepareStatement(sql);
			
			for (int i = 0; i < vals.size(); i++) {
				ps.setObject(i + 1, vals.get(i));
			}
			System.out.println("正删改 "+ps.toString());
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			exclose(null, ps, con);
		}

		return null;

	}

	// 对数据库的查询操作
	public static <T> List<T> Query(String sql, List<Object> vals, Class<T> clazz, Map<String, String> fields) {
		Connection connection = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
    
		List<T> result = new ArrayList<T>();

		  
		try {
			ps = connection.prepareStatement(sql);
			for (int i = 0; i < vals.size(); i++) {
			
				ps.setObject(i + 1, vals.get(i));
  
			}
        
			
			 System.out.println("查询sql语句执行141 行 "+ps.toString());
			rs = ps.executeQuery();

	
			 
			while (rs.next()) {
				T model = clazz.newInstance();

				for (Entry<String, String> entry : fields.entrySet()) {

					Field field = clazz.getDeclaredField(entry.getKey());
					field.setAccessible(true);
					
					field.set(model, rs.getObject(entry.getValue()));
					
					

				}
				result.add(model);

			}
		} catch (SQLException | InstantiationException | IllegalAccessException | NoSuchFieldException
				| SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			exclose(rs, ps, connection);
		}

		return result;
	}
	
	public static Integer selectCount(String sql,List<Object> vals) {
		Connection connection = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
			
			try {
				ps = connection.prepareStatement(sql);
				for(int i =0;i<vals.size();i++) {
				ps.setString(i+1, (String) vals.get(i));
				
				}
				System.out.println(ps.toString());
				rs=ps.executeQuery();
				while(rs.next()) {
					return rs.getInt("count");
				}
				
				
		    } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return null;
		
    
		
	}



}
